local yuanhu = fk.CreateSkill {

  name = "fk__yuanhu",

  tags = {  },

}

local U = require "packages/utility/utility"


yuanhu:addEffect(fk.EventPhaseStart, {

  anim_type = "support",

  can_trigger = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      return target == player and player:hasSkill(yuanhu.name) and player.phase == Player.Finish and not player:isNude()
    end
  end,
  on_trigger = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      self:doCost(event, target, player, data)
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      local _,dat = player.room:askForUseActiveSkill(player, "fk__yuanhu_active", "#fk__yuanhu-put", true)
      if dat then
        event:setCostData(self, dat)
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local dat = event:getCostData(self)
      room:moveCardTo(dat.cards, dat.interaction == "$Equip" and Card.PlayerEquip or Card.PlayerHand, dat.targets[1], fk.ReasonPut, yuanhu.name, nil, true, player.id)
      room:delay(600)
    end
  end,
})
yuanhu:addEffect(fk.AfterCardsMove, {

  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(yuanhu.name) then
      local use_id
      local e = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
      if e then
        if e.data.from == player and e.data.card.type == Card.TypeEquip then
          use_id = e.data.card:getEffectiveId()
        end
      end
      for _, move in ipairs(data) do
        if move.toArea == Card.PlayerEquip then
       
          if move.proposer == player or (#move.moveInfo == 1 and move.moveInfo[1].cardId == use_id) then
      
            return true
          end
        end
      end
    end
  end,
  on_trigger = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      self:doCost(event, target, player, data)
    else
      local list = {}
      local use_id
      local e = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
      if e then
        if e.data.from == player and e.data.card.type == Card.TypeEquip then
          use_id = e.data.card:getEffectiveId()
        end
      end
      for _, move in ipairs(data) do
        if move.toArea == Card.PlayerEquip then
          for _, info in ipairs(move.moveInfo) do
            if move.proposer == player or info.cardId == use_id then
              table.insert(list, {to = move.to, card = Fk:getCardById(info.cardId, true)})
            end
          end
        end
      end
      for _, dat in ipairs(list) do
        if not player:hasSkill(yuanhu.name) then break end
        self:doCost(event, dat.to, player, dat.card)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)

      return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      local dat = event:getCostData(self)
      room:moveCardTo(dat.cards, dat.interaction == "$Equip" and Card.PlayerEquip or Card.PlayerHand, dat.targets[1], fk.ReasonPut, yuanhu.name, nil, true, player)
      room:delay(600)
    else
      local str = Fk:translate(":"..data.name)
      local _, start = str:find("技能</b>")
      if start then
        str = str:sub(start)
      end
      if string.find(str, "【杀】") and not player.dead then
        local targets = table.filter(room.alive_players, function (p) return not p:isAllNude() end)
        if #targets > 0 then
          local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#fk__yuanhu-throw", yuanhu.name, false)
          local to = room:getPlayerById(tos[1])
          local card = room:askForCardChosen(player, to, "hej", yuanhu.name)
          room:throwCard(card, yuanhu.name, to, player)
        end
      end
      if string.find(str, "【闪】") and not player.dead and player:isWounded() then
        room:recover { num = 1, skillName = yuanhu.name, who = player, recoverBy = player }
      end
      if string.find(str, "伤害") and not player.dead then
        player:drawCards(1, yuanhu.name)
      end
      if string.find(str, "牌") and not target.dead then
        target:drawCards(1, yuanhu.name)
      end
      if string.find(str, "无效") and not target.dead then
        local tos = room:askForChoosePlayers(target, table.map(room.alive_players, Util.IdMapper), 1, 1, "#fk__yuanhu-damage", yuanhu.name, false)
        local to = room:getPlayerById(tos[1])
        room:damage { from = target, to = to, damage = 1, skillName = yuanhu.name }
      end
      if string.find(str, "距离") and not target.dead and target:isWounded() then
        room:recover { num = 1, skillName = yuanhu.name, who = target, recoverBy = player }
      end
    end
  end,
})

return yuanhu